/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * EditManga.java
 *
 * Created on Aug 14, 2011, 3:30:51 PM
 */
package azure;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.jdesktop.application.Action;

/**
 *
 * @author Gargravarr
 */
public class EditManga extends javax.swing.JFrame
{

    private Manga m;
    private ArrayList<Publisher> publishers = new ArrayList<Publisher>();
    SQLibrarian sql = new SQLibrarian();

    /** Creates new form EditManga */
    public EditManga()
    {
	super();
	initComponents();
	for (int year = 1970; year <= Calendar.getInstance().get(Calendar.YEAR); year += 1)
	{
	    jcboYear.addItem(year);
	}
	loadPublishers();
    }
    
    private void loadPublishers()
    {
	publishers = sql.getPublishers();
	jcboPublisher.removeAllItems();
	for (Publisher p : publishers)
	{
	    jcboPublisher.addItem(p.publisherName);
	}
    }

    public void setContent(Manga m)
    {
	this.m = m;
	jtMangaName.setText(m.mangaName);

	jcboPublisher.setSelectedItem(getPublisherForID(m.publisherID));

	jcboYear.setSelectedItem(m.publishedYear);
	jepDescription.setText(m.description);
    }

    public JPanel getInfoPanel()
    {
	return this.jpInfoPanel;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpInfoPanel = new javax.swing.JPanel();
        jlMangaName = new javax.swing.JLabel();
        jtMangaName = new javax.swing.JTextField();
        jlPublisher = new javax.swing.JLabel();
        jcboPublisher = new javax.swing.JComboBox();
        jlYear = new javax.swing.JLabel();
        jcboYear = new javax.swing.JComboBox();
        jlDescription = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jepDescription = new javax.swing.JEditorPane();
        jbCancel = new javax.swing.JButton();
        jbOK = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setName("Form"); // NOI18N
        setResizable(false);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jpInfoPanel.setName("jpInfoPanel"); // NOI18N
        jpInfoPanel.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jlMangaName.setText("Manga Name:"); // NOI18N
        jlMangaName.setName("jlMangaName"); // NOI18N
        jpInfoPanel.add(jlMangaName, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 26, -1, -1));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(azure.Main.class).getContext().getResourceMap(EditManga.class);
        jtMangaName.setText(resourceMap.getString("jtMangaName.text")); // NOI18N
        jtMangaName.setMinimumSize(new java.awt.Dimension(14, 55));
        jtMangaName.setName("jtMangaName"); // NOI18N
        jpInfoPanel.add(jtMangaName, new org.netbeans.lib.awtextra.AbsoluteConstraints(169, 20, 211, -1));

        jlPublisher.setText("Publisher:"); // NOI18N
        jlPublisher.setName("jlPublisher"); // NOI18N
        jpInfoPanel.add(jlPublisher, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 64, -1, -1));

        jcboPublisher.setEditable(true);
        jcboPublisher.setName("jcboPublisher"); // NOI18N
        jcboPublisher.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jcboPublisherFocusLost(evt);
            }
        });
        jpInfoPanel.add(jcboPublisher, new org.netbeans.lib.awtextra.AbsoluteConstraints(169, 58, 211, -1));

        jlYear.setText("Published Year:"); // NOI18N
        jlYear.setName("jlYear"); // NOI18N
        jpInfoPanel.add(jlYear, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, -1, -1));

        jcboYear.setEditable(true);
        jcboYear.setName("jcboYear"); // NOI18N
        jpInfoPanel.add(jcboYear, new org.netbeans.lib.awtextra.AbsoluteConstraints(169, 94, 211, -1));

        jlDescription.setText("Description:"); // NOI18N
        jlDescription.setName("jlDescription"); // NOI18N
        jpInfoPanel.add(jlDescription, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 130, -1, -1));

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setPreferredSize(new java.awt.Dimension(380, 150));

        jepDescription.setName("jepDescription"); // NOI18N
        jScrollPane1.setViewportView(jepDescription);

        jpInfoPanel.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 150, 380, 150));

        getContentPane().add(jpInfoPanel, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 400, 300));

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(azure.Main.class).getContext().getActionMap(EditManga.class, this);
        jbCancel.setAction(actionMap.get("closeForm")); // NOI18N
        jbCancel.setText("Cancel"); // NOI18N
        jbCancel.setName("jbCancel"); // NOI18N
        getContentPane().add(jbCancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(294, 302, -1, -1));

        jbOK.setAction(actionMap.get("saveChanges")); // NOI18N
        jbOK.setText("OK"); // NOI18N
        jbOK.setName("jbOK"); // NOI18N
        getContentPane().add(jbOK, new org.netbeans.lib.awtextra.AbsoluteConstraints(213, 302, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jcboPublisherFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jcboPublisherFocusLost
    checkPublisherExists();
}//GEN-LAST:event_jcboPublisherFocusLost

private void checkPublisherExists()
{
    boolean exists = false;
    for (Publisher p : publishers)
    {
	if (p.publisherName.equalsIgnoreCase(jcboPublisher.getSelectedItem().toString()))
	{
	    exists = true;
	    break;
	}
    }
    if (!exists)
    {
	createPublisher();
    }
}

private void createPublisher()
{
    String newPublisher = JOptionPane.showInputDialog(this.getInfoPanel(), "Create new entry for this publisher:", jcboPublisher.getSelectedItem().toString());
    if (newPublisher.length() > 0)
    {
	Publisher p = new Publisher(0, 0, newPublisher);
	sql.createPublisher(p);
	loadPublishers();
    }
}

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {
	/* Set the Nimbus look and feel */
	//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
	 * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
	 */
	try
	{
	    for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
	    {
		if ("Nimbus".equals(info.getName()))
		{
		    javax.swing.UIManager.setLookAndFeel(info.getClassName());
		    break;
		}
	    }
	}
	catch (ClassNotFoundException ex)
	{
	    java.util.logging.Logger.getLogger(EditManga.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	catch (InstantiationException ex)
	{
	    java.util.logging.Logger.getLogger(EditManga.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	catch (IllegalAccessException ex)
	{
	    java.util.logging.Logger.getLogger(EditManga.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	catch (javax.swing.UnsupportedLookAndFeelException ex)
	{
	    java.util.logging.Logger.getLogger(EditManga.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
	}
	//</editor-fold>

	/* Create and display the form */
	java.awt.EventQueue.invokeLater(new Runnable()
	{

	    public void run()
	    {
		new EditManga().setVisible(true);
	    }
	});
    }

    @Action
    public void saveChanges()
    {
	checkPublisherExists();
	m.mangaName = jtMangaName.getText();
	m.publisherID = getPublisherIDForName(jcboPublisher.getSelectedItem().toString());
	m.publishedYear = Integer.parseInt(jcboYear.getSelectedItem().toString());
	m.description = jepDescription.getText();
	m.isModified = true;
	sql.closeConn();
	this.dispose();
    }

    @Action
    public void closeForm()
    {
	this.dispose();
    }

    private String getPublisherForID(int publisherID)
    {
	for (Publisher p : publishers)
	{
	    if (p.publisherID == publisherID)
	    {
		return p.publisherName;
	    }
	}
	return "";
    }
    
    

    private int getPublisherIDForName(String publisherName)
    {
	for (Publisher p : publishers)
	{
	    if (p.publisherName == publisherName)
	    {
		return p.publisherID;
	    }
	}
	return 0;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbOK;
    private javax.swing.JComboBox jcboPublisher;
    private javax.swing.JComboBox jcboYear;
    private javax.swing.JEditorPane jepDescription;
    private javax.swing.JLabel jlDescription;
    private javax.swing.JLabel jlMangaName;
    private javax.swing.JLabel jlPublisher;
    private javax.swing.JLabel jlYear;
    private javax.swing.JPanel jpInfoPanel;
    private javax.swing.JTextField jtMangaName;
    // End of variables declaration//GEN-END:variables
}
